﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;
using System.IO;

namespace Task.Quartz.Execute.Holytax
{
    public class XmlFileBuilder
    {
        //生成的xml文件全名称
        private string _xmlFilePath = string.Empty;
        //XmlDocument
        private XmlDocument document = null;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="xmlFilePath">xml文件的全名称</param>
        public XmlFileBuilder(string xmlFilePath)
        {
            this._xmlFilePath = xmlFilePath;
            document = new XmlDocument();
            XmlDeclaration declaration = document.CreateXmlDeclaration("1.0", "GBK", null);
            document.AppendChild(declaration);
            XmlElement body = document.CreateElement("body");
            document.AppendChild(body); 
            XmlElement head = document.CreateElement("head");
            body.AppendChild(head);
            XmlElement data = document.CreateElement("data");
            body.AppendChild(data);
        }

        /// <summary>
        /// Header建造
        /// </summary>
        /// <param name="dictionary">键值对序列，分别表示节点名称和节点值</param>
        /// <returns></returns>
        public bool HeaderBuilder(Dictionary<string,string> dictionary)
        {
            try
            {
                XmlText text = null;
                XmlElement head = document.DocumentElement.SelectSingleNode("head") as XmlElement;
                foreach (KeyValuePair<string,string> kvp in dictionary)
                {
                    XmlElement eleTemp = document.CreateElement(kvp.Key);
                    text = document.CreateTextNode(kvp.Value);
                    eleTemp.AppendChild(text);
                    head.AppendChild(eleTemp);
                }
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 单张发票数据的创建
        /// </summary>
        /// <param name="dicInvoice">单张发票节点名称/属性值的KeyValuePair</param>
        /// <param name="rowIndex">发票的索引</param>
        /// <param name="ciphertextType">密文类型</param>
        /// <param name="dicGoods">物品节点名称/属性值得KeyValuePair的集合</param>
        /// <returns>是否创建节点成功</returns>
        public bool RowBuilder(Dictionary<string, string> dicInvoice, string rowIndex,string ciphertextType,
            List<Dictionary<string, string>> dicGoods)
        {
            try
            {
                XmlText text = null;
                XmlElement data = document.DocumentElement.SelectSingleNode("data") as XmlElement;
                XmlElement row = document.CreateElement("row");
                data.AppendChild(row);
                row.SetAttribute("id", rowIndex);
                foreach (KeyValuePair<string, string> kvp in dicInvoice)
                {
                    XmlElement eleTemp = document.CreateElement(kvp.Key);
                    text = document.CreateTextNode(kvp.Value);
                    eleTemp.AppendChild(text);
                    row.AppendChild(eleTemp);
                }
                if (ciphertextType.Equals("02"))
                {
                    XmlElement goodsInfo = document.CreateElement("wpxx");
                    row.AppendChild(goodsInfo);
                    for (int i = 0; i < dicGoods.Count; i++)
                    {
                        XmlElement goods = document.CreateElement("row");
                        goodsInfo.AppendChild(goods);
                        goods.SetAttribute("id", (i + 1).ToString());
                        foreach (KeyValuePair<string, string> kvpGoods in dicGoods[i])
                        {
                            XmlElement eleTempGoods = document.CreateElement(kvpGoods.Key);
                            text = document.CreateTextNode(kvpGoods.Value);
                            eleTempGoods.AppendChild(text);
                            goods.AppendChild(eleTempGoods);
                        }
                    }
                }
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 结束xml文件的创建，并保存document文档
        /// </summary>
        /// <returns>文件是否保存成功</returns>
        public bool XmlFileBuilderFinish()
        {
            try
            {
                document.Save(_xmlFilePath);
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }
    }
}
